package com.systemsjr.jrlib.richclient;

import javax.swing.JComponent;

import org.springframework.richclient.command.ActionCommand;
import org.springframework.richclient.command.CommandGroup;
import org.springframework.richclient.form.FilterForm;
import org.springframework.richclient.form.binding.swing.SwingBindingFactory;
import org.springframework.richclient.form.builder.TableFormBuilder;

public abstract class BaseItemFilterForm<T> extends FilterForm {

	private ActionCommand searchCommand;
	private ActionCommand clearCommand;
	protected BaseItemTable<T> table;
	protected SwingBindingFactory sbf;
	protected TableFormBuilder builder;
	
	public BaseItemFilterForm(String formId){
		super(formId);
		sbf = (SwingBindingFactory) getBindingFactory();
		builder = new TableFormBuilder(sbf);
		builder.setLabelAttributes("colSpec=right:pref");
	}
	
	@Override
	protected JComponent createFormControl() {
		
		doCreateFormControl();
		builder.getLayoutBuilder().cell(getCommands());
		builder.row();
		
		return builder.getForm();
	}
	
	protected abstract void doCreateFormControl();

	protected JComponent getCommands(){
		CommandGroup commands = CommandGroup.createCommandGroup(new ActionCommand[]{
				getSearchCommand(),
				getClearCommand()
		});
		
		return commands.createButtonBar();
	}

	public ActionCommand getSearchCommand() {
		if(searchCommand == null){
			searchCommand = new ActionCommand() {
				
				@Override
				protected void doExecuteCommand() {
					doSearch();
					
				}
			};
		}
		searchCommand.setLabel("Search");
		return searchCommand;
	}

	public ActionCommand getClearCommand() {
		if(clearCommand == null){
			clearCommand = new ActionCommand() {
				
				@Override
				protected void doExecuteCommand() {
					getFormModel().reset();
					
				}
			};
		}
		clearCommand.setLabel("Clear");
		return clearCommand;
	}

	
	protected abstract void doSearch();

}
